{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6bdb68a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from sklearn.feature_selection import SelectFromModel\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "from tsfresh import extract_features, extract_relevant_features\n",
    "from tsfresh.feature_extraction import settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "77e5e5e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load data\n",
    "\n",
    "X = pd.read_csv(\"occupancy.csv\", parse_dates=[\"date\"])\n",
    "y = pd.read_csv(\"occupancy_target.csv\", index_col=\"id\")[\"occupancy\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7d6e26b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████████████████████████████████████████████████████████| 10/10 [00:54<00:00,  5.48s/it]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(135, 968)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create and select features\n",
    "\n",
    "features = extract_relevant_features(\n",
    "    X,\n",
    "    y,\n",
    "    column_id=\"id\",\n",
    "    column_sort=\"date\",\n",
    ")\n",
    "\n",
    "features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b4d73915",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['light__sum_of_reoccurring_data_points',\n",
       "       'co2__fft_coefficient__attr_\"abs\"__coeff_0',\n",
       "       'co2__spkt_welch_density__coeff_2', 'co2__variance',\n",
       "       'temperature__c3__lag_1', 'temperature__abs_energy',\n",
       "       'temperature__c3__lag_2', 'temperature__c3__lag_3',\n",
       "       'co2__sum_of_reoccurring_data_points',\n",
       "       'light__spkt_welch_density__coeff_8',\n",
       "       'light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"var\"',\n",
       "       'light__agg_linear_trend__attr_\"slope\"__chunk_len_50__f_agg_\"var\"',\n",
       "       'light__agg_linear_trend__attr_\"intercept\"__chunk_len_10__f_agg_\"var\"'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# select features with lasso\n",
    "\n",
    "cls = LogisticRegression(\n",
    "    penalty=\"l1\", \n",
    "    solver=\"liblinear\",\n",
    "    random_state=10,\n",
    "    C=0.05,\n",
    "    max_iter=1000,\n",
    ")\n",
    "\n",
    "selector = SelectFromModel(cls)\n",
    "\n",
    "selector.fit(features, y)\n",
    "\n",
    "features = selector.get_feature_names_out()\n",
    "\n",
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "72ddeff0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'light': {'sum_of_reoccurring_data_points': None,\n",
       "  'spkt_welch_density': [{'coeff': 8}],\n",
       "  'agg_linear_trend': [{'attr': 'intercept', 'chunk_len': 50, 'f_agg': 'var'},\n",
       "   {'attr': 'slope', 'chunk_len': 50, 'f_agg': 'var'},\n",
       "   {'attr': 'intercept', 'chunk_len': 10, 'f_agg': 'var'}]},\n",
       " 'co2': {'fft_coefficient': [{'attr': 'abs', 'coeff': 0}],\n",
       "  'spkt_welch_density': [{'coeff': 2}],\n",
       "  'variance': None,\n",
       "  'sum_of_reoccurring_data_points': None},\n",
       " 'temperature': {'c3': [{'lag': 1}, {'lag': 2}, {'lag': 3}],\n",
       "  'abs_energy': None}}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# capture selected features for each time series\n",
    "\n",
    "kind_to_fc_parameters = settings.from_columns(selector.get_feature_names_out())\n",
    "\n",
    "kind_to_fc_parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f91d6eb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Feature Extraction: 100%|██████████████████████████████████████████████████████████████| 10/10 [00:04<00:00,  2.06it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(135, 13)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create selected features for each time series\n",
    "\n",
    "features = extract_features(\n",
    "    X,\n",
    "    column_id=\"id\",\n",
    "    column_sort=\"date\",\n",
    "    kind_to_fc_parameters=kind_to_fc_parameters,\n",
    ")\n",
    "\n",
    "features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "065af937",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temperature__c3__lag_1</th>\n",
       "      <th>temperature__c3__lag_2</th>\n",
       "      <th>temperature__c3__lag_3</th>\n",
       "      <th>temperature__abs_energy</th>\n",
       "      <th>light__sum_of_reoccurring_data_points</th>\n",
       "      <th>light__spkt_welch_density__coeff_8</th>\n",
       "      <th>light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"var\"</th>\n",
       "      <th>light__agg_linear_trend__attr_\"slope\"__chunk_len_50__f_agg_\"var\"</th>\n",
       "      <th>light__agg_linear_trend__attr_\"intercept\"__chunk_len_10__f_agg_\"var\"</th>\n",
       "      <th>co2__fft_coefficient__attr_\"abs\"__coeff_0</th>\n",
       "      <th>co2__spkt_welch_density__coeff_2</th>\n",
       "      <th>co2__variance</th>\n",
       "      <th>co2__sum_of_reoccurring_data_points</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11585.127934</td>\n",
       "      <td>11581.203590</td>\n",
       "      <td>11578.178807</td>\n",
       "      <td>30721.568703</td>\n",
       "      <td>2514.0</td>\n",
       "      <td>332.221295</td>\n",
       "      <td>21130.3425</td>\n",
       "      <td>-21130.3425</td>\n",
       "      <td>19305.1375</td>\n",
       "      <td>39343.666667</td>\n",
       "      <td>1523.529443</td>\n",
       "      <td>756.700664</td>\n",
       "      <td>14124.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10751.999610</td>\n",
       "      <td>10752.682508</td>\n",
       "      <td>10753.119812</td>\n",
       "      <td>29225.254374</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>34374.333333</td>\n",
       "      <td>350.067478</td>\n",
       "      <td>377.280895</td>\n",
       "      <td>13202.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10187.865226</td>\n",
       "      <td>10187.206963</td>\n",
       "      <td>10186.284040</td>\n",
       "      <td>28198.139950</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>31283.833333</td>\n",
       "      <td>91.896894</td>\n",
       "      <td>115.269298</td>\n",
       "      <td>20885.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9908.900224</td>\n",
       "      <td>9909.731388</td>\n",
       "      <td>9910.163947</td>\n",
       "      <td>27680.800184</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>29627.916667</td>\n",
       "      <td>42.394905</td>\n",
       "      <td>35.473216</td>\n",
       "      <td>18285.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>9705.989789</td>\n",
       "      <td>9706.404551</td>\n",
       "      <td>9706.707963</td>\n",
       "      <td>27299.097469</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>28618.250000</td>\n",
       "      <td>4.811303</td>\n",
       "      <td>27.539080</td>\n",
       "      <td>17670.500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   temperature__c3__lag_1  temperature__c3__lag_2  temperature__c3__lag_3  \\\n",
       "1            11585.127934            11581.203590            11578.178807   \n",
       "2            10751.999610            10752.682508            10753.119812   \n",
       "3            10187.865226            10187.206963            10186.284040   \n",
       "4             9908.900224             9909.731388             9910.163947   \n",
       "5             9705.989789             9706.404551             9706.707963   \n",
       "\n",
       "   temperature__abs_energy  light__sum_of_reoccurring_data_points  \\\n",
       "1             30721.568703                                 2514.0   \n",
       "2             29225.254374                                    0.0   \n",
       "3             28198.139950                                    0.0   \n",
       "4             27680.800184                                    0.0   \n",
       "5             27299.097469                                    0.0   \n",
       "\n",
       "   light__spkt_welch_density__coeff_8  \\\n",
       "1                          332.221295   \n",
       "2                            0.000000   \n",
       "3                            0.000000   \n",
       "4                            0.000000   \n",
       "5                            0.000000   \n",
       "\n",
       "   light__agg_linear_trend__attr_\"intercept\"__chunk_len_50__f_agg_\"var\"  \\\n",
       "1                                         21130.3425                      \n",
       "2                                             0.0000                      \n",
       "3                                             0.0000                      \n",
       "4                                             0.0000                      \n",
       "5                                             0.0000                      \n",
       "\n",
       "   light__agg_linear_trend__attr_\"slope\"__chunk_len_50__f_agg_\"var\"  \\\n",
       "1                                        -21130.3425                  \n",
       "2                                             0.0000                  \n",
       "3                                             0.0000                  \n",
       "4                                             0.0000                  \n",
       "5                                             0.0000                  \n",
       "\n",
       "   light__agg_linear_trend__attr_\"intercept\"__chunk_len_10__f_agg_\"var\"  \\\n",
       "1                                         19305.1375                      \n",
       "2                                             0.0000                      \n",
       "3                                             0.0000                      \n",
       "4                                             0.0000                      \n",
       "5                                             0.0000                      \n",
       "\n",
       "   co2__fft_coefficient__attr_\"abs\"__coeff_0  \\\n",
       "1                               39343.666667   \n",
       "2                               34374.333333   \n",
       "3                               31283.833333   \n",
       "4                               29627.916667   \n",
       "5                               28618.250000   \n",
       "\n",
       "   co2__spkt_welch_density__coeff_2  co2__variance  \\\n",
       "1                       1523.529443     756.700664   \n",
       "2                        350.067478     377.280895   \n",
       "3                         91.896894     115.269298   \n",
       "4                         42.394905      35.473216   \n",
       "5                          4.811303      27.539080   \n",
       "\n",
       "   co2__sum_of_reoccurring_data_points  \n",
       "1                         14124.000000  \n",
       "2                         13202.000000  \n",
       "3                         20885.666667  \n",
       "4                         18285.500000  \n",
       "5                         17670.500000  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cae5efb1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fsml",
   "language": "python",
   "name": "fsml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
